'''
Created on 16/10/2011

@author: nacho
'''
from cpuInstruction import CPUInstruction

class Program(object):
    '''
    classdocs
    '''


    def __init__(self, instructs=[], prio=5):
        '''
        Constructor
        '''
        self.instructions = instructs
        self.preference = prio
        self.atomizeAll()
        
    def addInstruction(self, anInstruction):
        
        self.instructions.append(anInstruction)
        
    def lenInstructions(self):
        
        return len(self.instructions)
    
    def code(self, instrNumber):
        
        return self.instructions[instrNumber]
    
    def isEmpty(self):
        
        return self.lenInstructions() == 0
    
    def fullCode(self):
        
        return self.instructions
    
    def priority(self):
        
        return self.preference
    
    def timer (self):
        
        return self.timer
    
    def  toMature(self):
        
        self.preference -=1
    
    
       
    def atomizeAll(self):
        
        finalList = []
        for i in self.instructions:
            
            while i.burst > 1 and i.needCPU():
                
                finalList.append(i.__class__(i.exe()))
                i.decreaseBurst()
                
            finalList.append(i.__class__(i.exe()))
            
            
        self.instructions = finalList 
    
    
if __name__ == '__main__':
    def printeable(cpu, josefo = "Josefo"):
        
        print(josefo)
    
    instr1 = CPUInstruction(printeable, 5)
    instr2 = CPUInstruction(printeable, 6)
    progr = Program([instr1, instr2], 3)
    print(len(progr.fullCode()))   
            